package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.entity.ClassTeacher;
import com.aier.common.common.SystemDateFormatUtil;


public class ClassTeacherRowMapper implements RowMapper<ClassTeacher> {

    private int type;

    public static final int AND_TEACHERNAME = 1;
    
    public static final int AND_TNAME = 2;
    
    public static final int AND_NAMES = 3;
    
    public static final int AND_ACCOUNT = 4;
    
    public ClassTeacherRowMapper() {
    }

    public ClassTeacherRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ClassTeacher mapRow(ResultSet rs, int rowNum) throws SQLException {
        ClassTeacher ct = parseClassTeacher(new ClassTeacher(), rs);
        ct.setCtime_str(SystemDateFormatUtil.formatDate(ct.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));

        switch (type) {
            case AND_TEACHERNAME:
                ct.setName_teacher(rs.getString("name_teacher"));
                ct.setGname(rs.getString("gname"));
                return ct;
            case AND_TNAME:
                ct.setName_teacher(rs.getString("name_teacher"));
                return ct;
            case AND_NAMES:
            	ct.setName_teacher(rs.getString("name_teacher"));
                ct.setName_classes(rs.getString("name_classes"));
                ct.setClogo(rs.getString("clogo"));
                return ct;
            case AND_ACCOUNT:
                ct.setName_teacher(rs.getString("name_teacher"));
                ct.setAccount(rs.getString("account"));
                return ct;
            default:
                return ct;
        }
        
    }

    public static ClassTeacher parseClassTeacher(ClassTeacher s, ResultSet rs) throws SQLException {
        return parseClassTeacher(s, rs, null);
    }

    public static ClassTeacher parseClassTeacher(ClassTeacher s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setCtid(rs.getLong(su+"ctid"));
        } catch (SQLException e) {
            s.setCtid(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        try {
            s.setIsadmin(rs.getShort(su+"isadmin"));
        } catch (SQLException e) {
            s.setIsadmin(null);
        }
       
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, ClassTeacher s) {
        par.put("ctid",s.getCtid());
        par.put("tid",s.getTid());
        par.put("cid",s.getCid());
        par.put("ctime",s.getCtime());
        par.put("isadmin",s.getIsadmin());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ClassTeacher s) {
        args.put("ctid",s.getCtid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final ClassTeacher s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "ctid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(ClassTeacher s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into class_teacher(");
        int count = 0;
        if (null != s.getTid()) {
            sql.append("tid,");
            count++;
        }

        if (null !=s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        if (null != s.getIsadmin()) {
            sql.append("isadmin,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, ClassTeacher s)
        throws SQLException {
        int count = 1;
        if (null != s.getTid()) {
            ps.setLong(count++,s.getTid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++, s.getCid());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }
        if (null != s.getIsadmin()) {
            ps.setShort(count++,s.getIsadmin());
        }
       


    }

}
